package com.hjm.app.fragment.home;

import android.content.Intent;
import android.graphics.BitmapFactory;
import android.os.Bundle;

import androidx.fragment.app.Fragment;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.hjm.app.R;
import com.hjm.app.activity.GoodsDetailActivity;
import com.hjm.app.activity.StoreActivity;
import com.hjm.app.adapter.HomeBannerAdapter;
import com.hjm.java.entity.GoodsListVO;
import com.hjm.java.entity.HomeBanner;
import com.hjm.java.entity.StoreNavListVO;
import com.hjm.ui.goods.GoodsItemList;
import com.hjm.ui.goods.adapter.GoodsItemAdapter;
import com.hjm.ui.goods.entity.GoodsItem;
import com.hjm.ui.home.HomeNav;
import com.hjm.ui.home.adapter.HomeNavAdapter;
import com.youth.banner.Banner;
import com.youth.banner.indicator.CircleIndicator;

import java.util.ArrayList;
import java.util.List;

public class FragmentHome extends Fragment {

    private Banner banner;
    private GoodsItemList goodsItemList;
    private HomeNav homeNav;
    private List<HomeBanner> bannerList;
    private List<GoodsItem> mGoodsList;
    private List<HomeNavAdapter.Nav> mNavList;
    private GoodsItemAdapter.Listener listener;
    private String TAG = "FragmentHome";

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        bannerList = new ArrayList<>();
        HomeBanner homeBanner = new HomeBanner();
        homeBanner.setImg(BitmapFactory.decodeResource(getResources(), R.drawable.yuan01));
        bannerList.add(homeBanner);
        homeBanner = new HomeBanner();
        homeBanner.setImg(BitmapFactory.decodeResource(getResources(), R.drawable.yuan02));
        bannerList.add(homeBanner);
        homeBanner = new HomeBanner();
        homeBanner.setImg(BitmapFactory.decodeResource(getResources(), R.drawable.yuan03));
        bannerList.add(homeBanner);

        if (mGoodsList == null)
            mGoodsList = new ArrayList<>();

//        mNavList = new ArrayList<>();
//        mNavList.add(new HomeNavAdapter.Nav(getString(R.string.title_supermarket), getResources().getDrawable(R.drawable.keli01)));
//        mNavList.add(new HomeNavAdapter.Nav(getString(R.string.title_pet_dress), getResources().getDrawable(R.drawable.keli01)));
//        mNavList.add(new HomeNavAdapter.Nav(getString(R.string.title_to_home), getResources().getDrawable(R.drawable.keli01)));
//        mNavList.add(new HomeNavAdapter.Nav(getString(R.string.title_to_up), getResources().getDrawable(R.drawable.keli01)));
//        mNavList.add(new HomeNavAdapter.Nav(getString(R.string.title_save_money), getResources().getDrawable(R.drawable.keli01)));
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View inflate = inflater.inflate(R.layout.fragment_home, container, false);
        banner = inflate.findViewById(R.id.banner);

        homeNav = inflate.findViewById(R.id.home_nav);

        goodsItemList = inflate.findViewById(R.id.home_goods_item);
        if (goodsItemList != null && mGoodsList != null)
            goodsItemList.setData(mGoodsList, 2,listener);
        useBanner();
//        useCenterNav();
        return inflate;
    }

    public void useBanner() {
        banner.addBannerLifecycleObserver(this)//添加生命周期观察者
                .setAdapter(new HomeBannerAdapter(bannerList))
                .setIndicator(new CircleIndicator(this.getContext()));
    }

//    public void useCenterNav() {
//        if (mNavList == null)
//            homeNav.setData(mNavList);
//    }

    public void setmGoodsList(List<GoodsListVO> goodsListVOList, GoodsItemAdapter.Listener listener) {
        Log.d(TAG, "setmGoodsList: 获得数据："+goodsListVOList.size());
        mGoodsList =  new ArrayList<>();
        GoodsItem goodsItem;
        for (GoodsListVO goodsListVO : goodsListVOList){
            goodsItem = new GoodsItem();
            goodsItem.setIconBitmap(goodsListVO.getIconBitmap());
            goodsItem.setName(goodsListVO.getName());
            goodsItem.setPrice(goodsListVO.getPrice());
            goodsItem.setId(goodsListVO.getId());
            mGoodsList.add(goodsItem);
        }
        this.listener = listener;
        if (goodsItemList != null){
            goodsItemList.setData(mGoodsList, 2,listener);
        }
    }

    public void setHomeNav(List<StoreNavListVO> storeNavListVOList) {
        mNavList = new ArrayList<>();
        HomeNavAdapter.Nav nav;
        for (StoreNavListVO storeNavListVO : storeNavListVOList){
            nav = new HomeNavAdapter.Nav();
            nav.setTitle(storeNavListVO.getName());
            nav.setId(storeNavListVO.getId());
            nav.setBitmap(storeNavListVO.getIconBitmap());
            mNavList.add(nav);
        }

        if (homeNav != null)
            homeNav.setData(mNavList,(id)->{
                Log.d(TAG, "setHomeNav: 点击了导航："+id);
                if (id == null)
                    return;
                Intent intent = new Intent(getContext(), StoreActivity.class);
                intent.putExtra("storeId", id);
                intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
                startActivity(intent);
            });
    }
}